package com.mini.service.impl;

import com.mini.entity.ConstantValue;
import com.mini.entity.GlobalException;
import com.mini.entity.Result;
import com.mini.entity.UserImg;
import com.mini.mapper.UserImgMapper;
import com.mini.service.UserImgService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mini.utils.AipOCR;
import com.mini.utils.FileUpLoadUtil;
//import com.mini.utils.FileUtil;
//import com.mini.utils.HttpUtil;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.boot.configurationprocessor.json.JSONArray;
import org.springframework.boot.configurationprocessor.json.JSONException;
import org.springframework.boot.configurationprocessor.json.JSONObject;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hy
 * @since 2021-04-28
 */
@Service
public class UserImgServiceImpl extends ServiceImpl<UserImgMapper, UserImg> implements UserImgService {

        //发送请求
    private String sendImgUrl(String imgUrl) throws JSONException, IOException {
        return AipOCR.verify(imgUrl);
    }
    @Override
    public boolean checkImage(String filePath) throws IOException, JSONException {
        return verifyContains((sendImgUrl(filePath)), "疫苗");
    }

    //调用图片上传接口，返回url给图片识别接口，图片识别接口识别并返回结果，返回两个数据，图片地址+是否可用
    private boolean verifyContains(String s1, String s2) {
        if (s1.contains(s2)) {
            return true;
        }
        return false;
    }
//    /**
//     * 组装http请求头
//     */
//    private static Map<String, String> buildHttpHeader() throws UnsupportedEncodingException {
//        String curTime = System.currentTimeMillis() / 1000L + "";
//        String param = "{\"location\":\"" + ConstantValue.LOCATION + "\",\"language\":\"" + ConstantValue.LANGUAGE + "\"}";
//        String paramBase64 = new String(Base64.encodeBase64(param.getBytes("UTF-8")));
//        String checkSum = DigestUtils.md5Hex(ConstantValue.API_KEY + curTime + paramBase64);
//        Map<String, String> header = new HashMap<String, String>();
//        header.put("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
//        header.put("X-Param", paramBase64);
//        header.put("X-CurTime", curTime);
//        header.put("X-CheckSum", checkSum);
//        header.put("X-Appid", ConstantValue.APPID);
//        return header;
//    }
//
//    //发送请求
//    private String sendImgUrl(String imgUrl) throws JSONException, IOException {
//        Map<String, String> header = buildHttpHeader();
//        byte[] imageByteArray = FileUtil.read(imgUrl);
//        String imageBase64 = new String(Base64.encodeBase64(imageByteArray), "UTF-8");
////        String result = HttpUtil.doPost1(ConstantValue.WEBOCR_URL, header, "image=" + URLEncoder.encode(imageBase64, "UTF-8"));
////        System.out.println("OCR WebAPI 接口调用结果：" + result);
//        JSONArray ret = new JSONArray();
//        ret.put(HttpUtil.doPost1(ConstantValue.WEBOCR_URL, header, "image=" + URLEncoder.encode(imageBase64, "UTF-8")));
//        String result = ret.toString();
//        return result;
//    }
//    //json解析
//    private String analyseJSON(String source) throws JSONException {
//        JSONObject jsonObject1 = new JSONObject(source);
//        String data = jsonObject1.getString("data");
//        System.out.println(data);
//        System.out.println("-------------");
//        jsonObject1 = new JSONObject(data);
//        JSONArray block = jsonObject1.getJSONArray("block");
//        System.out.println(block);
//        System.out.println("-------------");
//        JSONObject line = block.getJSONObject(0);
//        System.out.println(line);
//        System.out.println("-------------");
//        JSONArray linel = line.getJSONArray("line");
//        System.out.println(linel);
//        System.out.println("-------------");
//        JSONObject word = linel.getJSONObject(0);
//        System.out.println(word);
//        System.out.println("-------------");
//        JSONArray wordd = word.getJSONArray("word");
//        System.out.println(wordd);
//        System.out.println("-------------");
//        JSONObject content = wordd.getJSONObject(0);
//        System.out.println(content);
//        System.out.println("-------------");
//        String content1 = content.getString("content");
//        System.out.println(content1);
//        System.out.println("-------------");
//        return content1;
//    }
//json解析
//    private String analyseJSON(String source) throws JSONException {
//        JSONArray ret = new JSONArray();
//        ret.put(source);
//        String s = ret.toString();
//        return s;
//    }

//    //图片上传方法
//    @Override
//    public String upload(MultipartFile multipartFile) {
//        if(multipartFile == null) {
//            throw new GlobalException(Result.error());
//        }
//        String ret = FileUpLoadUtil.upload(multipartFile);
//        return ret;
//    }
}
